<?php
/**
 * User: Ason
 * 题库管理
 */

class Question extends Admin_controller {

	public $json = [ 'status'=>'error', 'error'=>'未知错误' ];	//返回状态
	
	public function __construct(){
		parent::__construct();
		parent::logged_check();
		$this->load->model( 'question_model', 'q' );
	}

	/**
	 * 题库列表
	 */
	public function index(){

		$this->data[ 'page_name' ] = '题库管理';
		$this->data[ 'questions'] = $this->q->get_question();

		$this->view( 'question/index' );
	}
	
	/**
	 * 添加新题目
	 * */
	public function add_question(){
		
		$post = $this->input->post( null, true );
/*		if( $this->q->add_question( $post ) ){
			$this->json[ 'status' ] = 'success';
			$this->json[ 'error' ] = '成功添加新题目！';
		}else{
			$this->json[ 'error' ] = '数据库操作失败！请重新尝试！';
		}
		
		echo json_encode( $this->json ); return ;
*/
		if( $this->q->add_question( $post ) ){
			$q_id = $this->db->insert_id();
			if( empty( $_FILES["attach"]["name"] ) ){
				$this->redirect( '题目添加成功！', '', 'question/index' );
			}else{
				
				$q_dir = $this->data['setting']['q_dir'] . $q_id;
				if( mkdir( $q_dir ) ){
					$attach_name = $_FILES['attach']['name'];
					$config[ 'upload_path'] = $q_dir;
					$config[ 'allowed_types' ] = $this->data['setting'][ 'question_types' ];
					$config[ 'file_name' ] = iconv( 'utf-8', 'gb2312', $attach_name );
					$config[ 'overwrite' ] = TRUE;
					$this->load->library( 'upload', $config );
					if( ! $this->upload->do_upload( 'attach' ) ){
						$this->load->helper( "WL_file" );
						del_dir( $q_dir );
						$this->redirect( '文件上传出错！', $this->upload->display_errors(), 'question/index' );
					}else{
						//上传成功，把附件名称写入数据库
						$attach = array( 'attach'=>$attach_name );
						$this->q->edit_question( $q_id, $attach );
						$this->redirect( '题目添加成功！', '', 'question/index' );
					}
				}else{
					$this->q->del_question( $q_id );
					$this->redirect( '无法创建题目文件夹！','', 'question/index' );
				}
			}
		}else{
			$this->redirect( '数据库错误！','写入数据库错误，请重试！', 'question/index' );
		}
	}
	/**
	 * 根据id删除题目及附件内容
	 * @param $id int 题目id
	 * */
	public function del_question( $id ){
		
		$id = (int)$id;
		if( $this->q->del_question( $id ) ){
			$q_dir = $this->data['setting']['q_dir'] . $id;
			$this->load->helper( 'WL_file' );
			if( is_dir( $q_dir ) ){
				if( ! del_dir( $q_dir ) ){
					$this->json[ 'error' ] = '无法删除相关文件，请手动操作！';
				}
			}
			$this->json[ 'status' ] = 'success';
			$this->json[ 'error' ] = '成功删除【id：'. $id .'】的题目';
		}else{
			$this->json[ 'error' ] = '无法删除数据库记录！';
		}
		
		echo json_encode( $this->json ); return ;
	}
	
	/**
	 * 修改题目
	 * @param int $id 题目id
	 */
	public function edit_question( $id ){
		
		$id = (int)$id;
		$data[ 'tpl_dir' ] = $this->data[ 'tpl_dir'];
		$data[ 'setting' ] = $this->data[ 'setting' ];
		$questions = $this->q->get_question( 'id', $id );
		$data['q'] = $questions[0];
		$this->load->view( 'question/part/edit_question', $data );
	}
	
	/**
	 * 修改题目post
	 * */
	public function do_edit_question( $id ){
		
		$post = $this->input->post( null, true );
		$id = (int)$id;
		if( $_FILES['attach']['name'] ){
			//如果有附件
			$q_dir = $this->data['setting']['q_dir'] . $id;
			if( ! is_dir( $q_dir ) ){
				mkdir( $q_dir );
			}
			$attach_name = $_FILES['attach']['name'];
			$config[ 'upload_path' ] = $q_dir;
			$config[ 'allowed_types' ] = $this->data['setting'][ 'question_types' ];
			$config[ 'file_name' ] = iconv( 'utf-8', 'gb2312', $attach_name );
			$config[ 'overwrite' ] = TRUE;
			$this->load->library( 'upload', $config );
			if( ! $this->upload->do_upload( 'attach' ) ){
				$this->load->helper( "WL_file" );
				del_dir( $q_dir );
				$this->redirect( '文件上传出错！', $this->upload->display_errors(), 'question/index' );
			}else{
				//上传成功，修改数据库
				$post[ 'attach' ] = $attach_name;
			}
		}
		if( $this->q->edit_question( $id, $post ) )
			$this->redirect( '题目修改成功！', '', 'question/index' );
		else
			$this->redirect( '数据库操作失败！请重新尝试！', '', 'question/index' );
	}

}